package hu.blok1.veilingsite.client.gui;


import hu.blok1.veilingsite.client.data.HomeService;
import hu.blok1.veilingsite.client.data.HomeServiceAsync;
import hu.blok1.veilingsite.shared.Product;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class homePageGUI {
	private final HomeServiceAsync HomeService = GWT.create(HomeService.class);
	private VerticalPanel vp;
	private DateTimeFormat dtf = DateTimeFormat.getFormat("dd-MM-yyyy HH:mm:ss");
	public homePageGUI(VerticalPanel a){
		vp = a;
		vp.clear();
		
		//panels
		HorizontalPanel horipanel1 = new HorizontalPanel();
		VerticalPanel vertipanelLeft = new VerticalPanel();
		vertipanelLeft.setWidth("250px");
		// start categories list
				
		Label catLabel = new Label("Categorieen ");
		catLabel.addStyleName("catlabel");
		vertipanelLeft.add(catLabel);
	    Hyperlink Linkboeken = new Hyperlink("Boeken", "searchResults//CATEGORY//Boeken");
	    Hyperlink Linkcd = new Hyperlink("CD's en DVD's", "searchResults//CATEGORY//CD's en DVD's");
	    Hyperlink Linkelek = new Hyperlink("Elektronica", "searchResults//CATEGORY//Elektronica");
	    Hyperlink Linkgames = new Hyperlink("Games", "searchResults//CATEGORY//Games");
	    Hyperlink Linkgereedschap = new Hyperlink("Gereedschappen", "searchResults//CATEGORY//Gereedschappen");
	    Hyperlink Linkvervoer = new Hyperlink("Vervoer en onderdelen", "searchResults//CATEGORY//Vervoer en onderdelen");
	    Hyperlink Linkmuziek = new Hyperlink("Muziekinstrumenten", "searchResults//CATEGORY//Muziekinstrumenten");
	    Hyperlink Linkdieren = new Hyperlink("Dieren en toebehoren", "searchResults//CATEGORY//Dieren en toebehoren");
	    Hyperlink Linksport= new Hyperlink("Sport en fitness", "searchResults//CATEGORY//Sport en fitness");
	    Hyperlink Linkdiversen= new Hyperlink("Diversen", "searchResults//CATEGORY//Diversen");

	    Tree tree = new Tree();
	    tree.addItem(Linkboeken);
	    tree.addItem(Linkcd);
	    tree.addItem(Linkelek);
	    tree.addItem(Linkgames);
	    tree.addItem(Linkgereedschap);
	    tree.addItem(Linkvervoer);
	    tree.addItem(Linkmuziek);
	    tree.addItem(Linkdieren);
	    tree.addItem(Linksport);
	    tree.addItem(Linkdiversen);
	    
	    vertipanelLeft.add(tree);
		// end categories list
	    
	    //search start
	    VerticalPanel vertipanelRight = new VerticalPanel();
	    HorizontalPanel horipanelsearch = new HorizontalPanel();
	    horipanelsearch.addStyleName("searchbar");
	    
		Label welkomlabel = new Label("Welkom bij de VeilingSite!");
		welkomlabel.addStyleName("welkomlabel");
	    
	    final TextBox searchtb = new TextBox();
	    searchtb.setWidth("450px");
	    Button searchbutton = new Button("Start zoekopdracht!");
	    searchbutton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				History.newItem("searchResults//SEARCH//"+ searchtb.getText());
			}
		});
	    horipanelsearch.add(searchtb);
	    horipanelsearch.add(searchbutton);
	    vertipanelRight.add(welkomlabel);
	    vertipanelRight.add(horipanelsearch);
	
	    // search end
	    
	    // add latest products
	    HorizontalPanel latestproductsHoriPanel = new HorizontalPanel();
	    
	    VerticalPanel productsleftpanel = new VerticalPanel();
	    VerticalPanel productsrightpanel = new VerticalPanel();
	    latestproductsHoriPanel.add(productsleftpanel);
	    latestproductsHoriPanel.add(productsrightpanel);
	    
	    // left table start
	 // cell table start
	    Label leftproducts = new Label("Nieuwste producten");
	    productsleftpanel.add(leftproducts);
	  
		// Create a CellTable.
		final List<Product> productListLeft = new ArrayList<Product>();
		final CellTable<Product> tableLeft = new CellTable<Product>();
		tableLeft.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);

	

		// Add a text column to show the name.
		TextColumn<Product> nameColumn = new TextColumn<Product>() {
			@Override
			public String getValue(Product object) {
				return object.getName();
			}
		};
		tableLeft.addColumn(nameColumn, "Titel");

		// Add a text column to show the id.
		TextColumn<Product> MinimumpriceColumn = new TextColumn<Product>() {
			@Override
			public String getValue(Product object) {
//				return String.valueOf(object.getMinimumPrize());
				return String.valueOf(object.getHighestBid());
			}
		};
		tableLeft.addColumn(MinimumpriceColumn, "Prijs");

		// Add a selection model to handle user selection.
		final SingleSelectionModel<Product> selectionModel = new SingleSelectionModel<Product>();
		tableLeft.setSelectionModel(selectionModel);
		selectionModel
				.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
					public void onSelectionChange(SelectionChangeEvent event) {
						Product selected = selectionModel
								.getSelectedObject();
						if (selected != null) {
							History.newItem("productSpecifications//"+ selected.getProductID());
						}
					}
				});

		// Set the total row count. This isn't strictly necessary, but it
		// affects
		// paging calculations, so its good habit to keep the row count up
		// to
		// date.
		tableLeft.setRowCount(productListLeft.size(), true);

		// Push the data into the widget.
		tableLeft.setRowData(0, productListLeft);
		productsleftpanel.add(tableLeft);
		// end cell table
		
		
		HomeService.Get10LatestProducts(new AsyncCallback<List<Product>>() {
					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Product(en) opvragen is mislukt");
					}

					@Override
					public void onSuccess(List<Product> result) {
						if (result.isEmpty()) {
							Window.alert("Er zijn helaas geen producten gevonden");
						} else {
							tableLeft.setRowData(result);
							tableLeft.setRowCount(10);
						}
					}
				});		
		
		
	    // left table end
	   
		 // start right table
		// cell table start
		    Label rightproducts = new Label("Deze producten sluiten bijna!");
		    productsrightpanel.add(rightproducts);
	
			// Create a CellTable.
			final List<Product> productListRight = new ArrayList<Product>();
			final CellTable<Product> tableRight = new CellTable<Product>();
			tableRight.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);

		

			// Add a text column to show the name.
			TextColumn<Product> nameColumnR = new TextColumn<Product>() {
				@Override
				public String getValue(Product object) {
					return object.getName();
				}
			};
			tableRight.addColumn(nameColumnR, "Titel");

			// Add a text column to show the price.
			TextColumn<Product> MinimumpriceColumnR = new TextColumn<Product>() {
				@Override
				public String getValue(Product object) {
//					return String.valueOf(object.getMinimumPrize());
					return String.valueOf(object.getHighestBid());
				}
			};
			tableRight.addColumn(MinimumpriceColumnR, "Prijs");
			
			// Add a text column to show the price.
			TextColumn<Product> endDateR = new TextColumn<Product>() {
				@Override
				public String getValue(Product object) {
					return String.valueOf(dtf.format(object.getEndDate()));
				}
			};
			tableRight.addColumn(endDateR, "Einddatum");

			// Add a selection model to handle user selection.
			final SingleSelectionModel<Product> selectionModelR = new SingleSelectionModel<Product>();
			tableRight.setSelectionModel(selectionModelR);
			selectionModelR
					.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
						public void onSelectionChange(SelectionChangeEvent event) {
							Product selectedR = selectionModelR
									.getSelectedObject();
							if (selectedR != null) {
								History.newItem("productSpecifications//"+ selectedR.getProductID());
							}
						}
					});

			// Set the total row count. This isn't strictly necessary, but it
			// affects
			// paging calculations, so its good habit to keep the row count up
			// to
			// date.
			tableRight.setRowCount(productListRight.size(), true);

			// Push the data into the widget.
			tableRight.setRowData(0, productListRight);
			
			
			
			
			
			productsrightpanel.add(tableRight);
			productsrightpanel.setStyleName("rightproductlist");
	
			
			// end cell table
			
			
			HomeService.Get10ClosestToEnding(new AsyncCallback<List<Product>>() {
						@Override
						public void onFailure(Throwable caught) {
							Window.alert("Product(en) opvragen is mislukt");
						}

						@Override
						public void onSuccess(List<Product> result) {
							if (result.isEmpty()) {
								Window.alert("Er zijn helaas geen producten gevonden");
							} else {
								tableRight.setRowData(result);
								tableRight.setRowCount(10);
							}
						}
					});		
			
			
		 
		 
		 
		 
		 
		 // end right table
			
			
			
		vertipanelRight.add(latestproductsHoriPanel);
		
	 
	    
	    
	    horipanel1.add(vertipanelLeft);
	    horipanel1.add(vertipanelRight);
		
		
		
		
		
		
		
		
		
	    vp.add(horipanel1);	
	}
}